package com.example.comitclx1.controller;

import com.example.comitclx1.pojo.Emp;
import com.example.comitclx1.pojo.Result;
import com.example.comitclx1.service.EmpService;
import com.example.comitclx1.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("登录{}",emp);
        Emp emp1=empService.login(emp);
        if(emp1!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",emp1.getId());
            claims.put("name",emp1.getName());
            claims.put("username",emp1.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            log.info(jwt);
            return Result.success(jwt);
        }
        return Result.error("账号或密码错误啊");
    }
}
